// pages/myCoupons/myCoupons.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    choo_modal:"none",
    radioValue: 1,
    checked: true,
    why:0,
    chooseCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
      type:options.type
    });
    this.setData({
      balance: wx.getStorageSync("balance")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onReady();
    var that = this;
    that.setData({
      token: wx.getStorageSync("token")
    });
    wx.request({
      url: app.globalData.url + "getUserCollectionList",
      method: "GET",
      data: {
        userId: wx.getStorageSync("userId"),
      },
      success: function (res) {
        var data = res.data;
        var choose = new Array();
        for(var i=0;i<data.data.length;i++){
            choose[i] = false;
        }
         that.setData({
           product: data.data,
           chooses:choose
         })
      }
    });
  },
  /**
  * 跳转到产品详情
  */
  details: function (res) {
    if(this.data.product[res.currentTarget.dataset.index].product.valid == true){
      var productid = res.currentTarget.dataset.productid;
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?productid=' + productid,
      })
    }else{
      wx.showToast({
        title: '该商品已经下架！',
        icon: "none"
      })
    }
   

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goDel:function(){
    var that = this;
    var chooses = this.data.chooses;
    var product = this.data.product;
    var notChoose = true;
    for(var i=0;i<chooses.length;i++){
      if(chooses[i]){
        notChoose = false;
        wx.request({
          url: app.globalData.url + "userCancelCollection",
          method: "GET",
          data: {
            userId: wx.getStorageSync("userId"),
            collectionIds: product[i].collectionId
          },
          success: function (res) {
            var data = res.data;
            if (data.result == 1) {
              wx.request({
                url: app.globalData.url + "getUserCollectionList",
                method: "GET",
                data: {
                  userId: wx.getStorageSync("userId"),
                },
                success: function (res) {
                  var data = res.data;
                  var choose = new Array();
                  for (var i = 0; i < data.data.length; i++) {
                    choose[i] = false;
                  }
                  that.setData({
                    product: data.data,
                    chooses: choose
                  })
                }
              });
            }
          }
        });
      }
    }
    if(notChoose){
      wx.showToast({
        title: "请选择收藏商品",
        icon: 'none'
      })
    }
    this.onShow();
    that.setData({
      chooseAll:false
    })
  },
  cancel:function(e){
    var that = this;
    wx.request({
      url: app.globalData.url + "userCancelCollection",
      method: "GET",
      data: {
        userId: wx.getStorageSync("userId"),
        collectionIds: e.currentTarget.dataset.collectionid
      },
      success: function (res) {
        var data = res.data;
        if (data.result == 1) {
          wx.showToast({
            title: "取消成功",
            icon: 'none'
          })
          that.onShow();
        } else {

        }
      }
    });
  },
  choose: function (e) {
    var count = this.data.chooseCount;
    if (this.data.chooses[e.currentTarget.dataset.index]) {
      this.data.chooses[e.currentTarget.dataset.index] = false;
      count--;
    } else {
      this.data.chooses[e.currentTarget.dataset.index] = true;
      count++;
    }
    var chooseAll = true;
    for (var i = 0; i < this.data.chooses.length; i++) {
      if (this.data.chooses[i] == false) {
        chooseAll = false;
      }
    }
    this.setData({
      chooses: this.data.chooses,
      chooseCount: count,
      chooseAll: chooseAll
    })

  },
  chooseAll: function () {
    if (this.data.chooseAll == false) {
      var chooseAll = true;
      var chooseCount = this.data.chooses.length;
      for (var i = 0; i < this.data.chooses.length; i++) {
        this.data.chooses[i] = true;
      }
      this.setData({
        chooses: this.data.chooses,
        chooseCount: chooseCount,
        chooseAll: chooseAll
      })
    } else {
      for (var i = 0; i < this.data.chooses.length; i++) {
        this.data.chooses[i] = false;
      }
      this.setData({
        chooses: this.data.chooses,
        chooseCount: 0,
        chooseAll: false
      })
    }

  },
  goOrderDetail: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?orderId=' + e.currentTarget.dataset.ordid + "&type=" + e.currentTarget.dataset.type
    })
  },
  dianji: function (e) {
    this.setData({
      why:1
    })
  },
  goNext: function (e) {
    this.setData({
      why: 0
    })
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  goLook: function (e) {
    wx.navigateTo({
      url: '/pages/productParentType/productParentType?productType=1&typeid=1'
    })
  },
})